<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<script type="text/javascript">
    require([ "dojo/_base/array", "dojo/_base/xhr", "dojo/request", "dojo/topic", "dojo/json" ],
            function(array, xhr, request, topic, JSON) {
                var deletItemsCount = 0;
                newsRestForm = function(formId) {
                    dijit.byId(formId).reset();
                    if (formId.indexOf('UFD') != -1) {
                        var rowid = jQuery("#newsGird").jqGrid('getGridParam', 'selrow');
                        var news = jQuery("#newsGird").jqGrid('getRowData', rowid);
                        dijit.byId('newsUFD').setValues(news);
                    }
                };
                topic.subscribe("news/insertOrUpdate", newsRestForm);
                var syncDB = function(url, jsonData) {
                    request.post(url, {
                        data : jsonData,
                        headers : {
                            'Content-Type' : "application/json; charset=utf-8"
                        },
                        timeout : 30000
                    }).then(function(response) {
                        topic.publish("notifyMsgTopic", {
                            message : response,
                            type : "message",
                            duration : 1000
                        });
                        var currentPage = jQuery('#newsGird').jqGrid('getGridParam', 'page');
                        var pageSize = jQuery('#newsGird').jqGrid('getGridParam', 'rowNum');
                        var totalCount = jQuery('#newsGird').jqGrid('getGridParam', 'records') - deletItemsCount;
                        var totalPages = Math.ceil(totalCount / pageSize);
                        jQuery("#newsGird").jqGrid('setGridParam', {
                            page : currentPage > totalPages ? totalPages : currentPage
                        }).trigger("reloadGrid");
                    }, function(error) {
                        alert(error);
                    });
                };
                newsSubmitForm = function(formId, type) {
                    var url = "news/insert";
                    if (type == 'u') {
                        url = "news/update";
                    }
                    var form = dijit.byId(formId);
                    if (form.isValid()) {
                        var json = JSON.stringify(form.getValues());
                        syncDB(url, json);
                        form.hide();
                        topic.publish("news/insertOrUpdate", formId);
                    } else {
                        form.validate();
                    }
                };
                newsDelete = function() {
                    var ids = jQuery("#newsGird").jqGrid('getGridParam', 'selarrrow') || [];
                    deletItemsCount = ids.length;
                    if (ids.length > 0) {
                        if (confirm('确认删除所选定的项？')) {
                            var news = {};
                            news.id = '';
                            array.forEach(ids, function(entry, i) {
                                news.id += entry + ",";
                            });
                            syncDB("news/delete", JSON.stringify(news));
                        }
                    } else {
                        alert('请选择至少一条数据!');
                    }
                };
                newsQuery = function(formId) {
                    var news = dijit.byId(formId).getValues();
                    jQuery("#newsGird").jqGrid('setGridParam', {
                        postData : news,
                        mtype : "post"
                    }).trigger('reloadGrid');
                };
            });
            jQuery("#issuer").attr("value",username);
</script>
<div data-dojo-type="dijit/Dialog" id="newsIFD" title="新增">
  <div class="dijitDialogPaneContentArea">
    <table>
      <tr>
        <td><label for="subject">新闻标题: </label></td>
        <td colspan="3"><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="subject" style="width: 99.5%"></td>
      </tr>
      <tr>
        <td><label for="author">作者: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="author"></td>
        <td><label for="status">是否激活: </label></td>
        <td>
            <select data-dojo-type="dijit/form/Select" name="status" style="width: 100%">
              <option value="1">激活</option>
              <option value="0">禁用</option>
            </select>
        </td>
      </tr>
      <tr>
      <td><label for="sectionid">所属栏目: </label></td>
        <td><div id="sectionidSelectINS"></div></td>
        <td><label for="sn">显示顺序: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="sn"></td>
      </tr>
      <tr>
	       <td><label for="content">内容: </label></td>
	       <td colspan="3"><input data-dojo-type="dijit/form/SimpleTextarea"  type="text" rows=3 cols=47  name="content"></td>
      </tr>
      <tr>
        <td><label for="isdeskimage">桌面新闻: </label></td>
        <td>
            <select data-dojo-type="dijit/form/Select" name="isdeskimage" style="width: 100%">
              <option value="1">是</option>
              <option value="0">否</option>
            </select>
        </td>
        <td><label for="author">发布人: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="issuer" id="issuer" readonly="readonly"></td>
      </tr>
      <tr>
        <td><label for="subjecticon">附件: </label></td>
        <td colspan="3"><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="subjecticon" onclick="uploadComponet(this,'subjecticonId');" style="width: 99.5%" readonly="readonly"/>
          <input id="subjecticonId" name="subjecticonId" data-dojo-type="dijit/form/ValidationTextBox" style="display: none;"/ >
          </td>
      </tr>
    </table>
  </div>
  <div class="dijitDialogPaneActionBar">
    <button data-dojo-type="dijit/form/Button" type="button" onclick="newsSubmitForm('newsIFD','i')">保存</button>
    <button data-dojo-type="dijit/form/Button" type="button" onclick="newsRestForm('newsIFD');">重置</button>
  </div>
</div>
<div data-dojo-type="dijit/Dialog" id="newsUFD" title="更新">
  <div class="dijitDialogPaneContentArea">
    <input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, readOnly: true" type="text"
      name="newsid" style="display: none;">
    <table>
      <tr>
        <td><label for="subject">新闻标题: </label></td>
        <td colspan="3"><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="subject" style="width: 99.5%"></td>
      </tr>
      <tr>
        <td><label for="author">作者: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="author"></td>
        <td><label for="status">是否激活: </label></td>
        <td>
            <select data-dojo-type="dijit/form/Select" name="status" style="width: 100%">
              <option value="1">激活</option>
              <option value="0">禁用</option>
            </select>
        </td>
      </tr>
      <tr>
      <td><label for="sectionid">所属栏目: </label></td>
        <td><div id="sectionidSelectUPS"></div></td>
        <td><label for="sn">显示顺序: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="sn"></td>
      </tr>
      <tr>
           <td><label for="content">内容: </label></td>
           <td colspan="3"><input data-dojo-type="dijit/form/SimpleTextarea"  type="text" rows=3 cols=47  name="content"></td>
      </tr>
      <tr>
        <td><label for="isdeskimage">桌面新闻: </label></td>
        <td>
            <select data-dojo-type="dijit/form/Select" name="isdeskimage" style="width: 100%">
              <option value="1">是</option>
              <option value="0">否</option>
            </select>
        </td>
        <td><label for="author">发布人: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="issuer" readonly="readonly"></td>
      </tr>
      <tr>
        <td><label for="subjecticon">附件: </label></td>
        <td colspan="3"><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="subjecticon" onclick="uploadComponet(this,'subjecticonIdUp');" style="width: 99.5%" readonly="readonly"/>
          <input id="subjecticonIdUp" name="subjecticonId" data-dojo-type="dijit/form/ValidationTextBox" style="display: none;"/ >
          </td>
      </tr>
    </table>
  </div>
  <div class="dijitDialogPaneActionBar">
    <button data-dojo-type="dijit/form/Button" type="button" onclick="newsSubmitForm('newsUFD','u')">保存</button>
    <button data-dojo-type="dijit/form/Button" type="button" onclick="newsRestForm('newsUFD');">重置</button>
  </div>
</div>
<fieldset>
  <legend>&nbsp;&nbsp;信息选项&nbsp;&nbsp;</legend>
  <form id="queryNewsForm" data-dojo-type="dijit/form/Form" method="post">
    <div style="margin-left: 5px;">
      <table style="width: 100%">
        <tr>
          <td width="8%"><label for="sectionid">所属栏目: </label></td>
          <td width="16%"><div id="sectionidSelectQU"></div></td>
          <td width="8%"><label for="createtime">创建时间: </label></td>
          <td width="16%">
            <input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="createtime" onClick="WdatePicker()" style="width: 40%">—<input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="exptime" onClick="WdatePicker()" style="width: 40%">          
          </td>
          <td width="8%"><label for="author">作者: </label></td>
          <td width="16%"><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="author" style="width: 90%"></td>
          </td>
        </tr>
        <tr>
          <td width="8%"><label for="subject">新闻标题: </label></td>
          <td width="16%"><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="subject" style="width: 90%"></td>
          <td width="8%"><label for="status">是否激活: </label></td>
          <td width="16%">
         <select data-dojo-type="dijit/form/Select" name="status" style="width: 86%">
          <option value="">全部</option>
          <option value="0">禁用</option>
          <option value="1">激活</option>
        </select>
          </td>
          <td  width="8%"><label for="issuer">发布人: </label></td>
          <td  width="16%"><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="issuer" style="width: 90%"></td>
        </tr>
      </table>
    </div>
  </form>
  <div style="padding: 5px;">
    <button id="newsInsertBtn" data-dojo-type="dijit/form/Button" type="submit" label="新增">
      <script type="dojo/method" event="onClick" args="evt">
        dijit.byId('newsIFD').show();
      </script>
    </button>
    <button id="newsDeleteBtn" data-dojo-type="dijit/form/Button" type="button" label="删除" onclick="newsDelete();"></button>
    <button id="newsUpdateBtn" data-dojo-type="dijit/form/Button" type="button" label="更新">
      <script type="dojo/method" event="onClick" args="evt">
        var ids = jQuery("#newsGird").jqGrid('getGridParam', 'selarrrow') || [];
        if (ids.length != 1) {
          alert('每次只能修改一条数据!');
        }else{
          var news = jQuery("#newsGird").jqGrid('getRowData', ids[0]);
          dijit.byId('newsUFD').setValues(news);
          dijit.byId('newsUFD').show();
        }
      </script>
    </button>
    <button id="newsQueryBtn" data-dojo-type="dijit/form/Button" type="button" label="查询"
      onclick="newsQuery('queryNewsForm');"></button>
  </div>
</fieldset>
<div id="gird">
  <table id="newsGird"></table>
  <div id="newsToolbar"></div>
</div>

<script src="resources/JSLIB/dojo/dojo/dojo.js" data-dojo-config="parseOnLoad:false, async:true"></script>
<script type="text/javascript">
    require([ "dijit/form/Select", "dojo/store/JsonRest", "dojo/json", "dojo/ready" ], function(Select, JsonRest,
            json, ready) {
        ready(function() {
            // create store instance referencing data from states.json
            var stateStore = new JsonRest({
                idProperty : "sectionid",
                target: "section/select"
            });
            // create Select widget, populating its options from the store
            var sectionidSelectQU = new Select({//query
                name : "sectionid",
                store : stateStore,
                style : "width: 90%;",
                labelAttr : "sectionname",
                maxHeight : -1
            }, "sectionidSelectQU");
            var sectionidSelectINS = new Select({//insert
                name : "sectionid",
                store : stateStore,
                style : "width: 100%;",
                labelAttr : "sectionname",
                maxHeight : -1
            }, "sectionidSelectINS");
            var sectionidSelectUPS = new Select({//insert
                name : "sectionid",
                store : stateStore,
                style : "width: 100%;",
                labelAttr : "sectionname",
                maxHeight : -1
            }, "sectionidSelectUPS");
            sectionidSelectQU.startup();
            sectionidSelectINS.startup();
            sectionidSelectUPS.startup();
        });
    });
</script>

<script type="text/javascript">
    jQuery("#newsGird").jqGrid({
        url : 'news/queryAsList',
        datatype : "json",
        autowidth : true,
        rownumbers : true,
        height : 'auto',
        colNames : [
          '新闻ID',
          '所属栏目',
          '新闻图片',
          '新闻标题',
          '作者',
          '创建时间',
          '失效时间',
          '重播次数',
          '浏览数',
          '发行者',
          '内容',
          '修改时间',
          '是否激活',
          '是否为桌面新闻',
          '是否新闻公告',
          '显示顺序'
        ],
        colModel : [ 
        {
          name : 'newsid',
          index : 'newsid',
          width : 60,
          key : true,
          hidden : true,
          align : 'center'
        },
        {
          name : 'sectionid',
          index : 'sectionid'
        },
        {
          name : 'subjecticon',
          hidden : true,
          index : 'subjecticon'
        },
        {
          name : 'subject',
          index : 'subject'
        },
        {
          name : 'author',
          index : 'author'
        },
        {
          name : 'createtime',
          index : 'createtime'
        },
        {
          name : 'exptime',
          hidden : true,
          index : 'exptime'
        },
        {
          name : 'replycounts',
          hidden : true,
          index : 'replycounts'
        },
        {
          name : 'viewcounts',
          index : 'viewcounts'
        },
        {
          name : 'issuer',
          index : 'issuer'
        },
        {
          name : 'content',
          index : 'content'
        },
        {
          name : 'updatetime',
          index : 'updatetime'
        },
        {
          name : 'status',
          index : 'status'
        },
        {
          name : 'isdeskimage',
          hidden : true,
          index : 'isdeskimage'
        },
        {
          name : 'isnotice',
          hidden : true,
          index : 'isnotice'
        },
        {
          name : 'sn',
          index : 'sn'
        }
        ],
        rowNum : 15,
        rowList : [ 15, 20, 25, 30 ],
        pager : '#newsToolbar',
        // sortname : 'username',
        recordpos : 'right',
        viewrecords : true,
        sortorder : "desc",
        jsonReader : {
            repeatitems : false
        },
        multiselect : true,
        shrinkToFit : false,
        caption : "新闻公告列表"
    });
    jQuery("#newsGird").jqGrid('navGrid', '#newsToolbar', {
        edit : false,
        add : false,
        del : false,
        search : false,
        refresh : true
    });
</script>
